package com.example.firstapp;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class MusicService extends Service {
    private static final String TAG = "MusicService";
    private MediaPlayer mediaPlayer;
    Timer timer;
    TimerTask task;
    Bundle bundle;

    class MyBinder extends Binder {
        //播放音乐
        public boolean play(String path) {
            try {
                if (mediaPlayer == null) {
                    //创建一个播放器
                    mediaPlayer = new MediaPlayer();
                    //指定参数为音频文件
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    //指定播放路径
                    mediaPlayer.setDataSource(path);
                    //准备播放
                    mediaPlayer.prepare();
                    mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start());
                } else {
                    int position = mediaPlayer.getCurrentPosition();
                    Log.i("music", "当前进度：" + position);
                    mediaPlayer.seekTo(position);
                    try {
                        mediaPlayer.prepare();
                    } catch (Exception ignored) {
                    }
                    mediaPlayer.start();
                }
                seekPlayProgress();
                return true;
            } catch (Exception ignored) {
            }
            return false;
        }

        public void seekPlayProgress() {
            /*1.获取当前歌曲的总时长*/
            final int duration = mediaPlayer.getDuration();

            //计时器对象
            timer = new Timer();
            try {
                task = new TimerTask() {
                    @Override
                    public void run() {
                        //开启线程定时获取当前播放进度
                        int currentPosition = mediaPlayer.getCurrentPosition();
                        //利用message给主线程发消息更新seekbar进度
                        Message ms = Message.obtain();
                        bundle = new Bundle();
                        bundle.putInt("duration", duration);
                        Log.i(TAG, "歌曲总长度" + duration);
                        bundle.putInt("currentPosition", currentPosition);
                        Log.i(TAG, "当前长度" + currentPosition);
                        //设置发送的消息内容
                        ms.setData(bundle);
                        //发送消息
                        Music02Activity.handler.sendMessage(ms);
                    }
                };
            } catch (Exception e) {
                timer.cancel();
                task.cancel();
                bundle.clear();
                bundle = null;
            }
            timer.schedule(task, 300, 500);
        }

        public boolean seekPlayProgress(int position) {
            if (mediaPlayer != null) {
                Log.i(TAG, "设置播放位置");
                mediaPlayer.seekTo(position);
                return true;
            } else {
                return false;
            }
        }

        //暂停播放
        public boolean pause() {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.pause();//暂停播放
                return true;
            } else if (mediaPlayer != null && (!mediaPlayer.isPlaying())) {
                mediaPlayer.start();//开始播放
                return true;
            }
            return false;
        }

        //停止播放
        public void stopPlay() {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
                timer.cancel();
                task.cancel();
            }
        }
    }

    public void onCreate() {
        super.onCreate();
    }

    //获取当前进度
    public int getCurrentProgress() {
        if (mediaPlayer != null) {
            return mediaPlayer.getCurrentPosition();
        }
        return 0;
    }

    public void onDestroy() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
}